/***************************************************************************
** 版权所有:   Copyright (c) 2020-2030
** 文件名称:  logenhancement.cpp
** 当前版本:  v1.0
** 作    者:
** 完成日期: 2024-07-15
** 修改记录:
** 修改日期:
** 版本号  :
** 修改人  :
** 修改内容:
***************************************************************************/

/*****************************头文件****************************************/

/*****************************宏定义****************************************/

/*****************************结构体或类型定义*******************************/

/*****************************全局变量****************************************/

/*****************************本地变量****************************************/

/*****************************函数或类声明****************************************/

/*****************************函数或类实现****************************************/
#include <iostream>
#include <unordered_map>
#include <deque>
#include <string>
#include <chrono>
#include <mutex>
#include <thread>

struct LogEntry {
    std::string message;
    unsigned int count;
    std::deque<std::chrono::system_clock::time_point> timestamps;
};

std::unordered_map<std::string, LogEntry> logEntries;
std::mutex logMutex;

void log(const std::string& functionName, int lineNumber, const std::string& message) {
    std::lock_guard<std::mutex> lock(logMutex);

    std::string key = functionName + std::to_string(lineNumber) + message;
    auto& entry = logEntries[key];

    auto now = std::chrono::system_clock::now();

    if (!entry.timestamps.empty() && std::chrono::duration_cast<std::chrono::seconds>(now - entry.timestamps.front()).count() <= 3) {
        entry.count++;
        entry.timestamps.push_front(now);
        if (entry.timestamps.size() > 20) {
            entry.timestamps.pop_back();
        }
    } else {
        entry.message = message;
        entry.count = 1;
        entry.timestamps.clear();
        entry.timestamps.push_front(now);
    }
}

void threadFunction() {
    for (int i = 0; i < 10; ++i) {
        char buf[100];
        snprintf(buf, sizeof(buf), "This is a test log.%d", i);
        log("threadFunction", __LINE__, buf);
        // std::this_thread::sleep_for(std::chrono::milliseconds(500));
        std::this_thread::sleep_for(std::chrono::milliseconds(rand() % 2000));
    }
}

int main() {
    std::thread t1(threadFunction);
    std::thread t2(threadFunction);
    std::thread t3(threadFunction);
    std::thread t4(threadFunction);
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    for (const auto& entry : logEntries) {
        std::cout << "Message: " << entry.second.message << ", Count: " << entry.second.count << std::endl;
        for (const auto& ts : entry.second.timestamps) {
            std::cout << "Timestamp: " << std::chrono::system_clock::to_time_t(ts) << std::endl;
        }
    }

    return 0;
}

/*****************************by extern "C"****************************************/

#ifdef __cplusplus
extern "C" {
#endif

/*****************************头文件****************************************/

/*****************************宏定义****************************************/

/*****************************结构体或类型定义*******************************/

/*****************************全局变量****************************************/

/*****************************本地变量****************************************/

/*****************************函数或类声明****************************************/

/*****************************函数或类实现****************************************/

#ifdef __cplusplus
}
#endif
